var form;
var dictId = getTempData('updateDictId');

$(function () {
    initLayUI(function () {
        initData();
    });
});

function initLayUI(cb) {
    layui.use(['form'], function () {
        form = layui.form;

        form.render();

        form.on('submit(dictEditForm)', function (data) {
            let dict = {
                "dictName": data.field.dictName,
                "dictType": data.field.dictType,
            };
            if (dictId !== undefined) {
                dict.dictId = dictId;
                updateDict(dict);
            } else {
                addDict(dict);
            }
            return false;
        });

        $("#cancelBtn").click(function () {
            layerUtil.closeLayer(parent.window.iframeIndex);
        });

        cb();
    });
}

function addDict(dict) {
    ajaxUtil.simplePost(dict, "/dict/add", function () {
        layerUtil.closeLayer();
        reloadParentTable('dictTable');
    });
}

function updateDict(dict) {
    ajaxUtil.simplePost(dict, "/dict/update", function () {
        layerUtil.closeLayer();
        reloadParentTable('dictTable');
    });
}

function initData() {
    if (dictId !== undefined) {
        ajaxUtil.simpleGet("/dict/queryDictById?dictId=" + dictId, function (data) {
            form.val('form', {
                "dictName": data.dictName,
                "dictType": data.dictType
            });
        });
    }
}
